<?php
require_once("../../../config.inc.php");
$isPopUp = true;
require_once("../../../app/_inc/include.php");

$errMsg = "";
$dataArea = "";
$type = "";
$contentType = "";
$docContent = "";
$fileName = "";
if(isset($_REQUEST["da"])){
	$dataArea = $_REQUEST["da"];
}else{
	$errMsg = _t("Data-Area undefined")."<br>";
}
if(isset($_REQUEST["fn"])){
	$fileName = $_REQUEST["fn"];
}else{
	$errMsg = _t("File unknown")."<br>";
}
if($errMsg == ""){
	//scarico il documento
	//$contentType = "application/pdf";
	//$contentType = "application/force-download";
	$sSQL = "SELECT mime_type FROM data_area_doc WHERE data_area='$dataArea' AND file_name='$fileName'";
	
	if($objSDAO->query($sSQL, "doc")){
		if($row = $objSDAO->fetch("doc")){
			$contentType = $row["mime_type"];
		}else{
			$errMsg .= _t("Document not found !")."<br>";
		}
	}else{
		$errMsg .= $sSQL." - ".$objSDAO->error;
	}	
}

if($errMsg == ""){
	if(file_exists(SERVER_FS_DOC."/mb_data_area/$dataArea/$fileName")){
		$fh = fopen(SERVER_FS_DOC."/mb_data_area/$dataArea/$fileName", "rb");
		$docContent = stream_get_contents($fh); 
		fclose($fh);
	}else{
		$errMsg .= "Read file failed";
	}
}	
if($errMsg != ""){
	headerDownload(strlen($errMsg), 'errMsg.txt');
	echo $errMsg;
	exit();
}
headerDownload(strlen($docContent), $fileName);
echo $docContent;
exit();
	